// 鲜花配送网站主JavaScript文件

// 处理导航栏在滚动时的效果
document.addEventListener('DOMContentLoaded', function() {
    // 获取导航栏元素
    const navbar = document.querySelector('.navbar');
    
    // 如果存在导航栏
    if (navbar) {
        // 监听滚动事件
        window.addEventListener('scroll', function() {
            if (window.scrollY > 50) {
                navbar.classList.add('navbar-scrolled');
            } else {
                navbar.classList.remove('navbar-scrolled');
            }
        });
    }

    // 处理移动端菜单切换
    const menuToggle = document.querySelector('.menu-toggle');
    const mobileMenu = document.querySelector('.mobile-menu');
    
    if (menuToggle && mobileMenu) {
        menuToggle.addEventListener('click', function() {
            mobileMenu.classList.toggle('hidden');
        });
    }

    // 商品数量增减控件
    const quantityControls = document.querySelectorAll('.quantity-control');
    
    if (quantityControls.length > 0) {
        quantityControls.forEach(control => {
            const minusBtn = control.querySelector('.minus-btn');
            const plusBtn = control.querySelector('.plus-btn');
            const quantityInput = control.querySelector('.quantity-input');
            
            if (minusBtn && plusBtn && quantityInput) {
                minusBtn.addEventListener('click', function() {
                    let value = parseInt(quantityInput.value);
                    if (value > 1) {
                        quantityInput.value = value - 1;
                    }
                });
                
                plusBtn.addEventListener('click', function() {
                    let value = parseInt(quantityInput.value);
                    quantityInput.value = value + 1;
                });
            }
        });
    }

    // 商品图片切换
    const thumbnails = document.querySelectorAll('.product-thumbnail');
    const mainImage = document.querySelector('.product-main-image');
    
    if (thumbnails.length > 0 && mainImage) {
        thumbnails.forEach(thumbnail => {
            thumbnail.addEventListener('click', function() {
                // 更新大图
                mainImage.src = this.src;
                
                // 移除所有缩略图的选中状态
                thumbnails.forEach(t => t.classList.remove('selected'));
                
                // 为当前缩略图添加选中状态
                this.classList.add('selected');
            });
        });
    }
});

// 轮播图功能
function initCarousel(carouselId, interval = 5000) {
    const carousel = document.getElementById(carouselId);
    if (!carousel) return;
    
    const slides = carousel.querySelectorAll('.carousel-slide');
    if (slides.length === 0) return;
    
    let currentSlide = 0;
    
    // 显示初始幻灯片
    slides[0].classList.add('active');
    
    // 自动切换幻灯片
    setInterval(() => {
        slides[currentSlide].classList.remove('active');
        currentSlide = (currentSlide + 1) % slides.length;
        slides[currentSlide].classList.add('active');
    }, interval);
}

// 初始化页面轮播图
document.addEventListener('DOMContentLoaded', function() {
    initCarousel('home-carousel');
}); 